package com.hw.paltfrom.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;



/**
 * @Desc 图片验证码生成工具
 * @Author Wisty
 * @Date 2018年10月11日
 */
public class ImageVerifyCodeUtil {
	
	private static Random random = new Random();
	
	private static final String IMAGE_CODE="imageCode_";

	
	// 图片验证码base64
	public static String createImgToBase64(String phone,Integer width,Integer height) throws IOException {
		RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
		BufferedImage bim = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D gc = bim.createGraphics();
		// 设置图片填充颜色
		gc.setColor(Color.yellow);
		gc.fillRect(0, 0, width, height);
		// 设置边框颜色
		gc.setColor(Color.pink);
		gc.drawRect(0, 0, width, height);
		// 产生4位随机数
		Random rand = new Random();
		StringBuffer sub = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			int m = rand.nextInt(9);
			sub.append(m);
		}
		
		int fontSize = height-4;
        Font font = new Font("Algerian", Font.ITALIC, fontSize);
        gc.setFont(font);
        char[] chars = sub.toString().toCharArray();
        for(int i = 0; i < 4; i++){
            AffineTransform affine = new AffineTransform();
            affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (width / 4) * i + fontSize/2, height/2);
            gc.setTransform(affine);
            gc.drawChars(chars, i, 1, ((width-10) / 4) * i + 5, height/2 + fontSize/2 - 10);
        }
		String imageCodeRedisKey=IMAGE_CODE+phone;
		//图片验证码放到缓存
		redisUtil.set(imageCodeRedisKey,sub.toString(), 0);
//		System.out.println(sub.toString());
		// 设置干扰线颜色
		gc.setColor(Color.cyan);
		for (int j = 0; j < 30; j++) {
			int x = rand.nextInt(width);
			int y = rand.nextInt(height);
			int x1 = rand.nextInt(6);
			int y1 = rand.nextInt(6);
			// 往图片里面画干扰线
			gc.drawLine(x, y, x + x1, y + y1);
		}

		// 添加噪点
		float yawpRate = 0.05f;// 噪声率
		int area = (int) (yawpRate * width * height);
		for (int i = 0; i < area; i++) {
			int x = rand.nextInt(width);
			int y = rand.nextInt(height);
			int rgb = getRandomIntColor();
			bim.setRGB(x, y, rgb);
		}

		// 创建编码对象
		// 创建字符流
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		// 写入字符流
		ImageIO.write(bim, "jpg", bs);
		// 转码成字符串
		String imgsrc = Base64.encodeBase64String(bs.toByteArray());
		bs.close();
		return imgsrc;
	}

	private static int getRandomIntColor() {
		int[] rgb = getRandomRgb();
		int color = 0;
		for (int c : rgb) {
			color = color << 8;
			color = color | c;
		}
		return color;
	}

	private static int[] getRandomRgb() {
		int[] rgb = new int[3];
		for (int i = 0; i < 3; i++) {
			rgb[i] = random.nextInt(255);
		}
		return rgb;
	}

	/**  
	 * @Desc  校验图片验证码
	 * @author Wisty
	 * @Date 2018年10月11日 下午11:24:13
	 */  
	public static Boolean verifyImageCode(String phone,String code){
		RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
		Boolean flag=true;
		String imageCodeRedisKey=IMAGE_CODE+phone;
		String verifyCode=(String) redisUtil.get(imageCodeRedisKey);
		if(verifyCode==null||!verifyCode.equals(code)){
			flag=false;
		}
		//校验成功或者失败都要删掉缓存中图片
		redisUtil.del(imageCodeRedisKey);
		return flag;
	}
}
